package com.umeng.analytics.pro;

import android.content.Context;
import android.text.TextUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;

public class af {
    public class a {
        private int a;
        private int b;
        private int c;
        private int d;
        private int e;
        private String f;
        private int g;
        private String h;
        private int i;
        private int j;
        private String k;
        private String l;
        private String m;
        private String n;
        private String o;

        a() {

            this.a = -1;
            this.b = -1;
            this.c = -1;
            this.d = -1;
            this.e = -1;
            this.f = null;
            this.g = -1;
            this.h = null;
            this.i = -1;
            this.j = -1;
            this.k = null;
            this.l = null;
            this.m = null;
            this.n = null;
            this.o = null;
        }

        a(bn arg3) {

            this.a = -1;
            this.b = -1;
            this.c = -1;
            this.d = -1;
            this.e = -1;
            this.f = null;
            this.g = -1;
            this.h = null;
            this.i = -1;
            this.j = -1;
            this.k = null;
            this.l = null;
            this.m = null;
            this.n = null;
            this.o = null;
            this.a(arg3);
        }

        public void a(bn arg2) {
            if (arg2 != null) {
                this.a = this.a(arg2, "defcon");
                this.b = this.a(arg2, "latent");
                this.c = this.a(arg2, "codex");
                this.d = this.a(arg2, "report_policy");
                this.e = this.a(arg2, "report_interval");
                this.f = this.b(arg2, "client_test");
                this.g = this.a(arg2, "test_report_interval");
                this.h = this.b(arg2, "umid");
                this.i = this.a(arg2, "integrated_test");
                this.j = this.a(arg2, "latent_hours");
                this.k = this.b(arg2, "country");
                this.l = this.b(arg2, "domain_p");
                this.m = this.b(arg2, "domain_s");
                this.n = this.b(arg2, "initial_view_time");
                this.o = this.b(arg2, "track_list");
            }
        }

        private int a(bn arg4, String arg5) {
            Object v0_2;
            int v0_1;
            int v1 = -1;
            if (arg4 != null) {
                try {
                    if (!arg4.f()) {
                        label_4:
                        v0_1 = v1;
                        return v0_1;
                    }

                    v0_2 = arg4.d().get(arg5);
                    if (v0_2 != null) {
                        if (TextUtils.isEmpty(((bo) v0_2).c())) {
                            return v1;
                        }
                        try {
                            label_14:
                            v0_1 = Integer.parseInt(((bo) v0_2).c().trim());
                        } catch (Exception v0) {
                            label_20:
                            v0_1 = v1;
                        }
                        return v0_1;
                    }
                    return v1;
                } catch (Exception v0) {
                    v0.printStackTrace();
                    return -1;
                }
            }
            return -1;
        }

        public int a(int arg3) {
            if (this.a != -1 && this.a <= 3 && this.a >= 0) {
                arg3 = this.a;
            }

            return arg3;
        }

        public long a(long arg6) {
            if (this.j != -1 && this.j >= 48) {
                arg6 = 3600000 * (((long) this.j));
            }

            return arg6;
        }

        public String a(String arg2) {
            if (this.n != null) {
                arg2 = this.n;
            }

            return arg2;
        }

        public boolean a() {
            boolean v0 = this.g != -1 ? true : false;
            return v0;
        }

        public int[] a(int arg7, int arg8) {
            int[] v0;
            int v5 = 90;
            int v4 = 2;
            int v1 = -1;
            if (this.d == v1 || !ca.a(this.d)) {
                v0 = new int[v4];
                v0[0] = arg7;
                v0[1] = arg8;
            } else {
                if (this.e == v1 || this.e < v5 || this.e > 86400) {
                    this.e = v5;
                }

                v0 = new int[v4];
                v0[0] = this.d;
                v0[1] = this.e * 1000;
            }

            return v0;
        }

        private String b(bn arg4, String arg5) {
            String v0_2;
            String v1 = null;
            if (arg4 != null) {
                try {
                    if (!arg4.f()) {
                        return v1;
                    }

                    Object v0_1 = arg4.d().get(arg5);
                    if (v0_1 == null) {
                        return v1;
                    }

                    if (TextUtils.isEmpty(((bo) v0_1).c())) {
                        return v1;
                    }

                    v0_2 = ((bo) v0_1).c();
                } catch (Exception v0) {
                    v0.printStackTrace();
                    v0_2 = v1;
                }

                v1 = v0_2;
            }

            return v1;
        }

        public int b(int arg3) {
            if (this.b != -1 && this.b >= 0 && this.b <= 1800) {
                arg3 = this.b * 1000;
            }

            return arg3;
        }

        public String b(String arg2) {
            if (this.o != null) {
                arg2 = this.o;
            }

            return arg2;
        }

        public boolean b() {
            boolean v0 = true;
            if (this.i != 1) {
                v0 = false;
            }

            return v0;
        }

        public int c(int arg3) {
            if (this.c == 0 || this.c == 1 || this.c == -1) {
                arg3 = this.c;
            }

            return arg3;
        }

        public String c(String arg2) {
            if (this.m != null) {
                arg2 = this.m;
            }

            return arg2;
        }

        public int d(int arg3) {
            if (this.g != -1 && this.g >= 90 && this.g <= 86400) {
                arg3 = this.g * 1000;
            }

            return arg3;
        }

        public String d(String arg2) {
            if (this.l != null) {
                arg2 = this.l;
            }

            return arg2;
        }

        public String e(String arg2) {
            if (this.k != null) {
                arg2 = this.k;
            }

            return arg2;
        }

        public String f(String arg2) {
            if (this.f != null && (bg.a(this.f))) {
                arg2 = this.f;
            }

            return arg2;
        }

        public String g(String arg2) {
            return this.h;
        }
    }

    private static final String a = ".imprint";
    private static final byte[] b = "pbl0".getBytes();
    private az c;
    private af.a d;
    private bn e;
    private static af f;
    private Context g;

    af(Context arg2) {
        this.d = new a();
        this.e = null;
        this.g = arg2;
    }

    public static af a(Context arg2) {
        af v0_1;
        Class v1 = af.class;
        synchronized (v1) {
            if (af.f == null) {
                v0_1 = new af(arg2);
                af.f = v0_1;
                v0_1.c();
            }

            v0_1 = af.f;
        }
        return v0_1;
    }

    private bn a(bn arg5, bn arg6) {
        if (arg6 != null) {
            Map v2 = arg5.d();
            Iterator v3 = arg6.d().entrySet().iterator();
            while (v3.hasNext()) {
                Object v0 = v3.next();
                if (((bo) ((Map.Entry) v0).getValue()).e()) {
                    v2.put(((Map.Entry) v0).getKey(), ((Map.Entry) v0).getValue());
                } else {
                    v2.remove(((Map.Entry) v0).getKey());
                }
            }

            arg5.a(arg6.g());
            arg5.a(this.a(arg5));
        }

        return arg5;
    }

    public String a(bn arg7) {
        StringBuilder v2 = new StringBuilder();
        Iterator v3 = new TreeMap(arg7.d()).entrySet().iterator();
        while (v3.hasNext()) {
            Object v0 = v3.next();
            v2.append(((Map.Entry) v0).getKey());
            if (((bo) ((Map.Entry) v0).getValue()).e()) {
                v2.append(((bo) ((Map.Entry) v0).getValue()).c());
            }

            v2.append(((bo) ((Map.Entry) v0).getValue()).f());
            v2.append(((bo) ((Map.Entry) v0).getValue()).i());
        }

        v2.append(arg7.b);
        return bw.a(v2.toString()).toLowerCase(Locale.US);
    }

    private boolean a(String arg2, String arg3) {
        boolean v0;
        if (arg2 != null) {
            v0 = arg2.equals(arg3);
        } else if (arg3 != null) {
            v0 = false;
        } else {
            v0 = true;
        }

        return v0;
    }

    public byte[] a(bo arg8) {
        int v6 = 4;
        ByteBuffer v0 = ByteBuffer.allocate(8);
        v0.order(null);
        v0.putLong(arg8.f());
        byte[] v1 = v0.array();
        byte[] v2 = af.b;
        byte[] v3 = new byte[v6];
        int v0_1;
        for (v0_1 = 0; v0_1 < v6; ++v0_1) {
            v3[v0_1] = ((byte) (v1[v0_1] ^ v2[v0_1]));
        }

        return v3;
    }

    public bn a() {
        bn v0_1;
        synchronized (this) {
            v0_1 = this.e;
        }
        return v0_1;
    }

    public void a(az arg1) {
        this.c = arg1;
    }

    public a b() {
        return this.d;
    }

    public void b(bn arg6) {
        String v1 = null;
        if (arg6 != null && (this.c(arg6))) {
            int v0 = 0;
            synchronized (this) {
                bn v4 = this.e;
                String v3 = v4 == null ? v1 : v4.j();
                bn v2 = v4 == null ? this.d(arg6) : this.a(v4, arg6);
                this.e = v2;
                if (v2 != null) {
                    v1 = v2.j();
                }

                if (!this.a(v3, v1)) {
                    v0 = 1;
                }
            }
            if (this.e == null) {
                return;
            }

            if (v0 == 0) {
                return;
            }

            this.d.a(this.e);
            if (this.c == null) {
                return;
            }

            this.c.a(this.d);
        }
    }

    public void c() {
        FileInputStream v2_1;
        InputStream v1_1;
        FileInputStream v1 = null;
        byte[] v2 = null;
        if (!new File(this.g.getFilesDir(), ".imprint").exists()) {
            return;
        }

        try {
            v1 = this.g.openFileInput(".imprint");
            v2 = bw.b(v1);
            if (v2 != null) {
                try {
                    bn v0_2 = new bn();
                    new cj().a(((cg) v0_2), v2);
                    this.e = v0_2;
                    this.d.a(v0_2);
                } catch (Exception v0_1) {
                    v0_1.printStackTrace();
                }
            }
        } catch (Exception v0_1) {
            v0_1.printStackTrace();
        } finally {
            bw.c(v1);
        }
    }

    private boolean c(bn arg8) {
        boolean v0;
        if (!arg8.j().equals(this.a(arg8))) {
            v0 = false;
        } else {
            Iterator v2 = arg8.d().values().iterator();
            while (true) {
                if (v2.hasNext()) {
                    Object v0_1 = v2.next();
                    byte[] v3 = bt.a(((bo) v0_1).i());
                    byte[] v4 = this.a(((bo) v0_1));
                    int v0_2 = 0;
                    while (true) {
                        if (v0_2 >= 4) {
                            break;
                        }

                        if (v3[v0_2] != v4[v0_2]) {
                            v0 = false;
                        } else {
                            ++v0_2;
                            continue;
                        }

                        return v0;
                    }
                } else {
                    return true;

                }
            }
        }

        return v0;
    }

    private bn d(bn arg6) {
        Map v2 = arg6.d();
        ArrayList v3 = new ArrayList(v2.size() / 2);
        Iterator v4 = v2.entrySet().iterator();
        while (v4.hasNext()) {
            Object v0 = v4.next();
            if (((bo) ((Map.Entry) v0).getValue()).e()) {
                continue;
            }

            ((List) v3).add(((Map.Entry) v0).getKey());
        }

        Iterator v1 = ((List) v3).iterator();
        while (v1.hasNext()) {
            v2.remove(v1.next());
        }

        return arg6;
    }

    public void d() {
        if (this.e != null) {
            try {
                bw.a(new File(this.g.getFilesDir(), ".imprint"), new cp().a(this.e));
            } catch (Exception v0) {
                v0.printStackTrace();
            }
        }
    }

    public boolean e() {
        return new File(this.g.getFilesDir(), ".imprint").delete();
    }
}

